const registerContainer = document.getElementById("register-container");
const registerLink = document.getElementById("register-link");

registerLink.addEventListener("click", function(event){
    event.preventDefault();
    registerContainer.classList.remove("hidden");
    loginContainer.classList.add("hidden");
});

const SendSmsButton = document.getElementById("SmsBtn");
SendSmsButton.addEventListener("click", function() {
    // 获取输入的手机号码
    const phone = document.getElementById("phone-number").value;
    // 判断手机号码是否为空
    if (phone === "") {
        alert("请输入手机号码！");
        return;
    }
    // 创建XMLHttpRequest对象
    const xhr = new XMLHttpRequest();
    // 设置请求的参数
    const data = "phone=" + encodeURIComponent(phone);
    xhr.open("POST", "https://api.fzuenactus.org.cn/api/sms/send");
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.withCredentials = true; // 开启跨域请求
    // 发送请求
    xhr.send(data);
    // 处理响应
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                // 根据后端返回的结果，显示提示信息
                const result = xhr.responseText;
                if (result === "success") {
                    alert("验证码已发送，请注意查收！\n短信签名为\"彩旗说\"");
                } else if (result === "failed") {
                    alert("验证码发送失败，请重试！");
                } else {
                    alert("服务器已成功处理，但短信发送异常，请稍后再试！");
                }
            } else {
                alert(xhr.status + " 响应，短信发送异常，请稍后再试！");
            }
        }
    };
})

const registerForm = document.getElementById("register-form");

registerForm.addEventListener("submit", function(event) {
    event.preventDefault();
    const formData = new FormData(event.target);
    const username = formData.get("username");
    const phoneNumber = formData.get("phone-number");
    const smsVerificationCode = formData.get("sms-verification-code");
    const password = formData.get("password");
    const confirmPassword = formData.get("confirm-password");
    // const invitationCode = formData.get("invitation-code");

    // 判断每个字段是否为空
    if (!username || !phoneNumber || !smsVerificationCode || !password || !confirmPassword) {
        alert("请填写完整的注册信息！");
        return;
    }

    if (confirmPassword!==password){
        alert("两次密码不一致！");
        return;
    }

    // 发送注册请求
    // fetch('/api/user/register', {
    fetch('https://snake-konginchrist.com/api/user/register', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            username: username,
            phoneNumber:phoneNumber,
            smsVerificationCode:smsVerificationCode,
            password: password,
            // invitationCode:invitationCode,
        }),
        mode: 'cors',
        credentials: 'include'
    })
    .then(response => {
        if (response.ok) {//注册成功
            console.log(response);
            response.json().then(data=>{
                alert(data.message);// 显示注册成功信息
                registerContainer.classList.add("hidden");
                loginContainer.classList.remove("hidden");
            });
        }else {
            console.log(response);
            response.json().then(error => {
                alert(error.message);// 显示其他错误信息，例如用户名已存在、两次密码不一致，邀请码不存在
            });
        }
    })
    .catch(error => {
        console.error(error);
    });
});